#################################
## REPLICATION MATERIAL #########
# Function for Balance Tests ####
# Handi Li, June 2025

# load package dplyr

balance <- function(samp.bal,treat,names){
  treatnum <- nrow(filter(samp.bal, treat == 1))
  controlnum <- nrow(filter(samp.bal, treat == 0))
  
  n <- ncol(samp.bal)
  diff_mean <- array(NA,c(1,n))
  se <- array(NA,c(1,n))
  t <- array(NA,c(1,n))
  p <- array(NA,c(1,n))
  
  for (i in 1:n){
    diff_mean[i] <- mean(filter(samp.bal, treat == 1)[,i],na.rm=T) - 
      mean(filter(samp.bal, treat == 0)[,i],na.rm=T)
    se[i] <- sqrt(var(filter(samp.bal, treat == 1)[,i],na.rm=T)/treatnum +
                    var(filter(samp.bal, treat == 0)[,i],na.rm=T)/controlnum)
    t[i] <- diff_mean[i]/se[i]
    p[i] <- 2*pt(-abs(t[i]), df=pmin(treatnum,controlnum)-1)
  }
  t.bal <- t(rbind(names,diff_mean,t,se,p))
  colnames(t.bal) <- c("variable","diff_mean","t","se","p")
  t.bal[,-1] <- round(as.numeric(t.bal[,-1]),4)
  t.bal
}
